#include "opensslhelper.h"

#ifndef ENABLE_GMSSL
#include <openssl/x509.h>
#include <openssl/x509_vfy.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
#endif

OpensslHelper::OpensslHelper(QObject *parent)
  : QObject{parent}
{

}

QString OpensslHelper::ASN1TIME2QString(ASN1_TIME *time)
{
#ifndef ENABLE_GMSSL
  BIO* bio = BIO_new(BIO_s_mem());
  if(!bio) {
    qFatal("libcrypto: failed to allocate memory");
    exit(-1);
  }

  if (ASN1_TIME_print(bio, time) <= 0) {
      qFatal("Failed to print ASN1_TIME");
      BIO_free(bio);
      exit(-1);
  }

  BUF_MEM *buffer;
  BIO_get_mem_ptr(bio, &buffer);
  BUF_MEM_grow(buffer, buffer->length + 1);
  buffer->data[buffer->length] = 0;
  QString from_val(buffer->data);
  BIO_free(bio);
  return from_val;
#else
  return QString();
#endif
}
